import { createRouter, createWebHashHistory } from "vue-router";
import { mainStore } from "../store/index"
import { ElNotification } from 'element-plus'
const router = createRouter({
    history: createWebHashHistory(),
    routes: [
        {
            path: "/",
            redirect: {
                name: "Login"
            }
        },
        {
            path: "/Login",
            name: "Login",
            component: () => import("../views/Login.vue"),
        },
        {
            path: "/Manager",
            name: "Manager",
            component: () => import("../views/Manager.vue"),
            meta: {
                requireAuth: true
            },
            children: [
                {
                    name: "DataView",
                    path: "DataView",
                    component: () => import("../views/DataView.vue"),
                    meta:{
                        title: "首页",
                        requireAuth: true
                    }
                },
                {
                    path: "CategoryInfoList",
                    name: "CategoryInfoList",
                    component: () => import("../views/CategoryInfo/CategoryInfoList.vue"),
                    meta: {
                        title: "分类列表",
                        requireAuth: true
                    }
                },
                {
                    path: "AddCategoryInfo",
                    name: "AddCategoryInfo",
                    component: () => import("../views/CategoryInfo/AddCategoryInfo.vue"),
                    meta: {
                        title: "增加分类",
                        autoClose: true,
                        requireAuth: true
                    }
                },
                {
                    path: "EditCategoryInfo/:id",
                    name: "EditCategoryInfo",
                    component: () => import("../views/CategoryInfo/EditCategoryInfo.vue"),
                    meta: {
                        title: "编辑分类信息",
                        autoClose: true,
                        requireAuth: true
                    }
                },
                {
                    path: "FoodInfoList",
                    name: "FoodInfoList",
                    component: () => import("../views/FoodInfo/FoodInfoList.vue"),
                    meta: {
                        title: "菜品信息",
                        requireAuth: true
                    }
                },
                {
                    path: "AddFoodInfo",
                    name: "AddFoodInfo",
                    component: () => import("../views/FoodInfo/AddFoodInfo.vue"),
                    meta: {
                        title: "增加菜品",
                        autoClose: true,
                        requireAuth: true
                    }
                }
                ,
                {
                    path: "EditFoodInfo/:id",
                    name: "EditFoodInfo",
                    component: () => import("../views/FoodInfo/EditFoodInfo.vue"),
                    meta: {
                        title: "编辑菜品信息",
                        autoClose: true,
                        requireAuth: true
                    }
                }
                ,
                {
                    path: "UserInfoList",
                    name: "UserInfoList",
                    component: () => import("../views/UserInfo/UserInfoList.vue"),
                    meta: {
                        title: "用户信息",
                        requireAuth: true
                    }
                },
                {
                    path: "AddressInfoList",
                    name: "AddressInfoList",
                    component: () => import("../views/AddressInfo/AddressInfoList.vue"),
                    meta: {
                        title: "地址信息",
                        requireAuth: true
                    }
                }
                ,
                {
                    path: "OrderInfoList",
                    name: "OrderInfoList",
                    component: () => import("../views/OrderInfo/OrderInfoList.vue"),
                    meta: {
                        title: "订单信息",
                        requireAuth: true
                    }
                }
                ,
                {
                    path: "CommentInfoList",
                    name: "CommentInfoList",
                    component: () => import("../views/CommentInfo/CommentInfoList.vue"),
                    meta: {
                        title: "评论信息",
                        requireAuth: true
                    }
                }
                ,
                {
                    path: "AdminInfoList",
                    name: "AdminInfoList",
                    component: () => import("../views/AdminInfo/AdminInfoList.vue"),
                    meta: {
                        title: "管理员信息",
                        requireAuth: true
                    }
                }
                ,
                {
                    path: "EditAdminInfo/:id",
                    name: "EditAdminInfo",
                    component: () => import("../views/AdminInfo/EditAdminInfo.vue"),
                    meta: {
                        title: "编辑管理员",
                        autoClose: true,
                        requireAuth: true
                    }
                },
                {
                    path: "AddAdminInfo",
                    name: "AddAdminInfo",
                    component: () => import("../views/AdminInfo/AddAdminInfo.vue"),
                    meta: {
                        title: "增加管理员",
                        autoClose: true,
                        requireAuth: true
                    }
                },
                {
                    path: "EditAdminPassword",
                    name: "EditAdminPassword",
                    component: () => import("../views/AdminInfo/EditAdminPassword.vue"),
                    meta: {
                        title: "增加管理员密码",
                        autoClose: true,
                        requireAuth: true
                    }
                }

            ]
        }
    ]
});
// 页面跳转之前的操作
router.beforeEach((to, from, next) => {
    const store = mainStore();
    if (to.meta.requireAuth) {
        //需要登录以后才能进
        if (store.token) {
            //说明有token，放行
            next();
        } else {
            ElNotification({
                type: "warning",
                title: "系统提示",
                message: "您还没有登录，请先去登录"
            });
            next({
                name: "Login"
            });
        }

    } else {
        //不需要登录
        next();
    }
});


// 跳转页面之后的操作
router.afterEach((to, from) => {
    // console.log(to);
    const store = mainStore();
    if (to.meta.title) {
        store.addHistory({
            title: to.meta.title,
            routeName: to.name
        })
    }
    if (from.meta.autoClose) {
        store.removeHisotryByRouteName(from.name);
    }


});


export default router;